<template>
  <div style="width: 100%;height: 500px;padding-top:0;margin: 0 ">
    <div class="pageheader">
      <div class="pageheader-1">
        <i class="el-icon-user-solid"></i><label>慢性病政策设置</label>
      </div>
      <div style="height: 100%;position: absolute;top: 20px;left: 350px">
        <el-form :model="input" ref="key">
        <el-input
          style="width: 200px;position: absolute;top: 5px;left: 0px"
          size="small"
          placeholder="请输入年份"
          suffix-icon="el-icon-search"
          maxlength="20"
          v-model="input.input3">
        </el-input>
        <el-button type="primary" class="searchbtn" @click="find('key')" plain>查询</el-button>
        </el-form>
        <el-button type="primary" v-show="list[1].children[3].showadd" @click="dialogFormVisible=true" class="add">添加</el-button>
        <!--添加表单-->
        <el-dialog title="添加" @close="this.reload" :visible.sync="dialogFormVisible">
          <el-form :rules="rules" :model="form" ref="addAction">
            <el-form-item prop="year" label="年份" :label-width="formLabelWidth">
              <el-input v-model="form.years" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="blibli" label="报销比例" :label-width="formLabelWidth">
              <el-input v-model="form.blibli" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="top" label="封顶线" :label-width="formLabelWidth">
              <el-input v-model="form.top" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="add ('addAction')">确 定</el-button>
          </div>
        </el-dialog>
        <!--修改表单-->
        <el-dialog title="修改" :visible.sync="editdep">
          <el-form :rules="rules" :model="e_form" ref="editdep1">
            <el-form-item prop="year" label="年份" :label-width="formLabelWidth">
              <el-input v-model="e_form.years" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="blibli" label="报销比例" :label-width="formLabelWidth">
              <el-input v-model="e_form.blibli" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="top" label="封顶线" :label-width="formLabelWidth">
              <el-input v-model="e_form.top" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="editdep = false">取 消</el-button>
            <el-button type="primary" @click="edit ('editdep1')">确 定</el-button>
          </div>
        </el-dialog>
        <!--提示 -->
        <el-dialog
          title="提示"
          :visible.sync="openelete"
          width="30%"
          center>
          <span>你确定要删除该条记录吗?</span>
          <span slot="footer" class="dialog-footer">
    <el-button @click="openelete = false">取 消</el-button>
    <el-button type="primary" @click="delete_data" >确 定</el-button>
  </span>
        </el-dialog>
      </div>

    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%;height: 80%">

      <el-table-column
        prop="years"
        label="年份">
      </el-table-column>
      <el-table-column
        prop="blibli"
        label="报销比例/%">
      </el-table-column>
      <el-table-column
        prop="top"
        label="封顶线">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作">
        <template slot-scope="scope">
          <el-button v-show="list[1].children[3].showedit" type="primary" @click="handleClick(scope.row)" >修改</el-button>
          <el-button v-show="list[1].children[3].showdelete" type="danger" @click="opendelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页 -->
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        page-size="5"
        :total="total"
        @current-change="page">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  inject: ['reload', 'list'],
  methods: {
    // 查询功能
    find (key) {
      const _this = this
      if (this.input.input3 !== '') {
        axios.get('http://localhost:8088/findByAreaidF/' + this.input.input3).then(function (resp) {
          _this.tableData = resp.data
          _this.total = 1
        })
      } else {
        this.reload()
      }
    },
    // 添加
    add (addAction) {
      this.$refs[addAction].validate((valid) => {
        axios.post('http://localhost:8088/saveZheng', this.form).then(function (resp) {
        })
        this.$message({
          message: '添加成功！',
          type: 'success'
        })
        this.reload()
      })
    },
    // 修改
    edit (editdep1) {
      this.$refs[editdep1].validate((valid) => {
        axios.post('http://localhost:8088/saveZheng', this.e_form).then(function (resp) {
        })
      })
      this.$message({
        message: '修改成功！',
        type: 'success'
      })
      this.reload()
    },
    // 读取当前行的数据
    handleClick (row) {
      console.log(row)
      this.e_form = row
      this.editdep = true
    },
    // 删除
    opendelete (row) {
      this.openelete = true
      this.form = row
    },
    delete_data () {
      axios.post('http://localhost:8088/deleteZheng/' + this.form.id).then(function (resp) {
      })
      this.$message({
        message: '删除成功！',
        type: 'success'
      })
      this.reload()
    },
    // 分页模块
    page (currentpage) {
      const _this = this
      axios.get('http://localhost:8088/findZhengAll/' + currentpage + '/5').then(function (resp) {
        console.log(resp.data)
        _this.tableData = resp.data.content
        _this.total = resp.data.totalElements
      })
    }
  },
  // 接受后台数据
  created () {
    const _this = this
    axios.get('http://localhost:8088/findZhengAll/1/5').then(function (resp) {
      _this.tableData = resp.data.content
      _this.total = resp.data.totalElements
    })
  },
  //  数据绑定
  data () {
    return {
      total: null,
      input: {
        input3: ''
      },
      tableData: null,
      dialogFormVisible: false,
      editdep: false,
      openelete: false,
      form: {
        id: '',
        years: '',
        blibli: '',
        top: ''
      },
      e_form: {
        id: '',
        years: '',
        blibli: '',
        top: ''
      },
      formLabelWidth: '100px',
      rules: {
        years: [
          { required: true, message: '请输入区域编号', trigger: 'blur' }
        ],
        blibli: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ],
        top: [
          { required: true, message: '请选择等级', trigger: 'blur' }
        ]
      }
    }
  }
}
</script>
